package com.etc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.spi.DirStateFactory.Result;

import com.etc.entity.Skill;
import com.etc.entity.SkillFactory;
import com.etc.tools.DBTools;

public class SkillDAO {

	DBTools tool = new DBTools();
	Connection conn = tool.getConnection();
	
	//添加技能
	public void addSkill(Skill skill) {
		String sql = "insert into T_ITEM_SKILL values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, skill.getSkillId());
			ps.setString(2, skill.getSkillName());
			ps.setInt(3, skill.getConsumBlue());
			ps.setString(4, skill.getSkillDesc());
			ps.setString(5, skill.getType());
			ps.setInt(6, skill.getSkillCd());
			ps.setInt(7, skill.getAttrHeal());
			ps.setInt(8, skill.getAttrImage());
			ps.setInt(9, skill.getAttrAttack());
			ps.setInt(10, skill.getAttrCrit());
			ps.setInt(11, skill.getAttrDefense());
			ps.setInt(12, skill.getAttrSpeed());
			ps.setInt(13, skill.getDuration());
			ps.setString(14, skill.getClassId());
			int confirm = ps.executeUpdate();
			if(confirm==1) System.out.println("技能插入成功");
		} catch (SQLException e) {
			System.out.println("技能插入异常");
			e.printStackTrace();
		}
		
	}
	
	//根据id查找技能，一个技能
	public Skill findSkillById(String skillId) {
		String sql = "select * from t_item_skill where skill_Id = ?";
		PreparedStatement ps = null;
		String skillType = null ;
		Skill skill = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, skillId);
			ResultSet set = ps.executeQuery();
			set.next();
			if(set.getString("type").equals("攻击技能")) {
				skillType = "SkillAttack";
			}
			if(set.getString("type").equals("增伤技能")) {
				skillType = "SkillBuff";
			}if(set.getString("type").equals("减益技能")) {
				skillType = "SkillDebuff";
			}
			skill = SkillFactory.createSkill(skillType, set.getString("skill_id"), 
					set.getString("skill_name"), set.getString("skill_desc"), 
					set.getInt("skill_cd"), set.getInt("consum_blue"), set.getString("type"),
					set.getInt("attr_heal"), set.getInt("attr_image"), set.getInt("attr_attack"),
					set.getInt("attr_crit"), set.getInt("attr_defense"), set.getInt("attr_speed"),
					set.getInt("duration"), set.getString("class_id"));
			
		} catch (SQLException e) {
			System.out.println("根据id查找技能查询异常");
			e.printStackTrace();
		}
		
		return skill;
	}
	
	//根据职业Id查找技能，比如说大唐这个职业有五个技能，需要返回五个技能对象，所以返回集合
	public ArrayList<Skill> findSkillByClass(String classId) {
		String sql = "select * from t_item_skill where class_id = ?";
		PreparedStatement ps = null;
		String skillType = null ;
		ArrayList<Skill> skills = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, classId);
			ResultSet set = ps.executeQuery();
			while(set.next()) {
				if(set.getString("type").substring(0, 1).equals("攻")) {
					skillType = "SkillAttack";
				}else if(set.getString("type").substring(0, 1).equals("增")) {
					skillType = "SkillBuff";
				}else if(set.getString("type").substring(0, 1).equals("减")) {
					skillType = "SkillDebuff";
				}
				//由于使用了继承，子类重写了父类的方法，所以需要调用对应的子类对象，这个时候就用到了工厂模式，动态创建对象
				Skill skill = SkillFactory.createSkill(skillType, set.getString("skill_id"), 
						set.getString("skill_name"), set.getString("skill_desc"), 
						set.getInt("skill_cd"), set.getInt("consum_blue"), set.getString("type"),
						set.getInt("attr_heal"), set.getInt("attr_image"), set.getInt("attr_attack"),
						set.getInt("attr_crit"), set.getInt("attr_defense"), set.getInt("attr_speed"),
						set.getInt("duration"), set.getString("class_id"));
				skills.add(skill); //把这个技能添加进集合里面
			}
		} catch (SQLException e) {
			System.out.println("根据职业Id查找技能查询异常");
			e.printStackTrace();
		}
		return skills;
		
	}
}
 
